package com.example.administrator.xxnapp.user;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.example.administrator.xxnapp.BaseActivity;
import com.example.administrator.xxnapp.R;
import com.example.administrator.xxnapp.bean.UserBean;
import com.example.administrator.xxnapp.http.HttpUrl;
import com.example.administrator.xxnapp.util.Config;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.mdruby.baselibrary.utils.ObjectStoreUtil;
import cn.mdruby.pickphotovideoview.MediaModel;
import cn.mdruby.pickphotovideoview.PickConfig;
import cn.mdruby.pickphotovideoview.PickPhotoView;

public class UserInfoActivity extends BaseActivity implements IViewUser{
    @BindView(R.id.act_user_info_IV_headIamge)
    public ImageView mIVHead;
    @BindView(R.id.act_user_info_TV_Name)
    TextView mTVName;
    @BindView(R.id.act_user_info_TV_PhoneNum)
    TextView mTVPhone;
    private boolean isEdit;
    private UserBean mUser;
    private UserPresenter mPresenter;

    @Override
    public void initView() {
        ButterKnife.bind(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_user_info;
    }

    @Override
    public void initBeforeDatas() {
        mUser = (UserBean) ObjectStoreUtil.getObject(mContext, Config.Key.USER_OBJECT_KEY);
        mPresenter = new UserPresenter(this,new UserBean());
    }

    @Override
    public void setListeners() {
        mTVName.setText(mUser.getUserName());
        mTVPhone.setText(mUser.getUserPhone());
        RequestOptions options = new RequestOptions();
        options.transform(new CircleCrop());
        options.error(R.mipmap.ic_launcher);
        Glide.with(this)
                .load(HttpUrl.BASE_URL_BASE+mUser.getHeadImage())
                .apply(options)
                .into(mIVHead);
    }

    @OnClick(R.id.act_user_info_RL_HeadImage)
    public void headIamge(View view){
        new PickPhotoView.Bulid(mContext).setCount(1)
                .showVideo(false)
                .showCamera(true)
                .setCanCrop(true)
                .setCanZip(true)
                .useLocalCamera(true)
                .start();
    }

    @OnClick(R.id.act_user_info_TV_Edit)
    public void edit(View view){
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
            switch (requestCode){
                case PickConfig.RequestCode.SELECT_PHOTO:
                    List<MediaModel> mediaModels = (List<MediaModel>) data.getSerializableExtra(PickConfig.KEY.MEDIA_FILE_DATA);
                    if (mediaModels.size()>0){
                        RequestOptions options = new RequestOptions();
                        options.transform(new CircleCrop());
                        Glide.with(mContext).load(mediaModels.get(0).getFile().getPath()).apply(options).into(mIVHead);
                        mPresenter.uploadHeadImage(mediaModels.get(0).getFile(),mUser.getUserId());
                    }
                    break;
            }
        }
    }

    @Override
    public void uploadImageSuccess(String path) {
        Log.e(TAG, "uploadImageSuccess: "+path );
        mUser.setHeadImage(path);
        ObjectStoreUtil.saveObject(mContext,Config.Key.USER_OBJECT_KEY,mUser);
    }

    @Override
    public void uploadImageError(int resultCode, String message) {
        Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
    }
}
